﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class UserRoleComponent
    {
        public bool UserRole_Add(UserRole userRole)
        {
            return FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserRole_Add", userRole) > 0;
        }

        public bool UserRole_BulkAdd(List<UserRole> userRoles)
        {
            foreach (UserRole item in userRoles)
            {
                FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserRole_Add", item);
            }
            return true;
        }

        public bool UserRole_Delete(string userKeyid, string roleKeyid)
        {
            return FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_UserRole_Delete", null, userKeyid, roleKeyid) > 0;
        }

        /// <summary>
        /// 为用户批量移除角色
        /// </summary>
        /// <param name="userKeyid">用户Keyid</param>
        /// <param name="roleKeyid">角色Keyid集合</param>
        /// <returns></returns>
        public bool UserRole_BulkDelete(string userKeyid, string[] roleKeyids)
        {
            for (int i = 0; i < roleKeyids.Length; i++)
            {
                this.UserRole_Delete(userKeyid, roleKeyids[i]);
            }
            return true;
        }

        public bool UserRole_Exists(string userKeyid, string roleKeyid)
        {
            return (int)FishBLLHelper.CallSpExecuteScalar("sgrf_permission_UserRole_Exists", null, userKeyid, roleKeyid) > 0;
        }

        public List<Role> UserRole_GetRoleByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Role>("sgrf_permission_UserRole_GetRoleByUserKeyid", null, userKeyid);
        }

        public List<Role> UserRole_GetUnmappedRoleByUserKeyid(string userKeyid, string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "RoleCode", DbType.String, " AND RoleCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "RoleName", DbType.String, " AND RoleName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Grade", DbType.Int32, " AND Grade = '{0}' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<Role>("sgrf_permission_UserRole_GetUnmappedRoleByUserKeyid", null, userKeyid, filterConditions);
        }


        /// <summary>
        /// 查询文档审核岗位下的所有用户
        /// </summary>
        /// <returns></returns>

        public List<UserRole> UserRole_GetByUserKeyid()
        {
            return FishBLLHelper.CallSpGetDataItemList<UserRole>("sgrf_permission_UserRole_GetUserKeyid", null);
        }

    }
}
